package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.SplashEntity;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.view.view.SplashView;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2016/12/24.
 */

public class SplashPresenter implements Presenter {

    private UseCase splash;
    private UseCase splashImage;
    private SplashView splashView;
    private AppCompatActivity activity;

    @Inject
    public SplashPresenter(@Named("checkLoginUseCase")UseCase splash,
                           @Named("splashImageUseCase")UseCase splashImage,
                           AppCompatActivity appCompatActivity) {
        this.splash = splash;
        this.splashImage = splashImage;
        this.activity = appCompatActivity;
    }

    public void setView(SplashView view) {
        this.splashView = view;
    }

    public void checkLoginStatus() {
        splash.execute(new SplashSubscriber(activity), null);
    }

    public void getSplashImage() {
        splashImage.execute(new SplashImageSubscriber(activity), null);
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        splash.unSubscribe();
        splashImage.unSubscribe();
    }

    private class SplashSubscriber extends BaseSubscriber<Boolean> {

        SplashSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Boolean result) {
            super.onNext(result);
            splashView.loginStatus(result);
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            splashView.loginStatus(false);
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.check_login_status_fail);
        }
    }

    private class SplashImageSubscriber extends BaseSubscriber<SplashEntity> {

        public SplashImageSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public String addErrorMessage() {
            return null;
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
        }

        @Override
        public void onError(Throwable e) {
        }
    }
}
